<template>
  <el-dialog
    title="添加管理员"
    width="80%"
    :append-to-body="true"
    :visible.sync="showTag"
  >
    <el-row>
      <el-col :span="15">
        <el-form :model="form" size="mini" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="姓名">
                <el-input v-model="form.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="身份证">
                <el-input v-model="form.idcard"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="手机">
                <el-input v-model="form.phone"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="邮箱">
                <el-input v-model="form.email"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col v-if="!(data && data.id)" :span="12">
              <el-form-item label="密码">
                <el-input v-model="form.pass"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-col>
      <el-col :span="1"></el-col>
      <el-col :span="8">
        <el-form :model="form" size="mini" label-width="80px">
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :file-list="fileList"
              list-type="picture-card"
              :on-success="onSuccess"
              :on-remove="onRemove"
              :limit="1"
            >
              <i class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
    <div slot="footer">
      <el-button size="mini" @click="hide">取消</el-button>
      <el-button type="primary" size="mini" :loading="appLoading" @click="sub">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import { FIELDS } from "../../../lib/tables/admin";
import { UploadMixin } from "../../../lib/mixins/upload";
import { AppLoadingMixin } from "../../../lib/mixins/app-loading";

export default {
  name: "AdminAddDialog",
  mixins: [UploadMixin, AppLoadingMixin],
  data() {
    return {
      fileList: [],
      form: {},
      data: {},
      showTag: false
    };
  },
  methods: {
    show(data) {
      this.data = data;
      this.setForm();
      this.showTag = true;
    },
    setForm() {
      const { data } = this;
      if (data && data.id) {
        FIELDS.forEach(r => {
          data[r] && this.$set(this.form, r, data[r]);
        });
        if (data.ava) {
          this.fileList = [
            {
              url: data.ava
            }
          ];
        }
      }
    },
    hide() {
      this.form = Object.create(null);
      this.fileList.splice(0);
      this.showTag = false;
    },
    onSuccess(response, file, fileList) {
      response.errno === 0 && (this.fileList = fileList);
    },
    onRemove(file, fileList) {
      this.fileList = fileList;
      if (!fileList || !fileList.length) this.form.ava = "";
    },
    async sub() {
      const form = { ...this.form };
      const { fileList } = this;
      if (fileList && fileList.length) {
        fileList.forEach(r => {
          form.ava =
            (r.response && r.response.data && r.response.data.url) || r.url;
        });
      }
      if (form.id) {
        if (await this.$adminService.update(form)) {
          this.hide();
          this.$message.success("修改成功");
          this.$emit("ok");
        }
        return;
      }
      if (await this.$adminService.add(form)) {
        this.hide();
        this.$message.success("添加成功");
        this.$emit("ok");
      }
    }
  }
};
</script>

<style scoped></style>
